package com.zhiwei.gateway.controller;

import com.zhiwei.common.constant.ErrorEnum;
import com.zhiwei.common.constant.ResultResponseMsg;
import com.zhiwei.gateway.controller.vo.UserLoginVo;
import com.zhiwei.gateway.controller.vo.UserLogoutVo;
import com.zhiwei.gateway.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * function: 用户服务控制器
 * author: zhiwei_yang
 * time: 2020/2/15-19:41
 */
@RestController
@RequestMapping("/user")
@Api
public class UserController {

    @Autowired
    private LoginService loginService;

    /**
     * 用户登陆服务
     * @param userLoginVo 用户登陆信息
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "/login", notes = "用户登陆")
    public ResultResponseMsg login(@RequestBody UserLoginVo userLoginVo, HttpSession httpSession){
        String username = userLoginVo.getUsername();
        String password = userLoginVo.getPassword();
        String captcha = userLoginVo.getCaptcha();
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(captcha)){
            return ResultResponseMsg.buildResultMsg(ErrorEnum.PARAM, null);
        }
        String jwtToken = loginService.userLogin(username,password,httpSession.getId(), captcha);
        if(StringUtils.isEmpty(jwtToken)){
            return ResultResponseMsg.buildResultMsg(ErrorEnum.PARAM, null);
        }
        return ResultResponseMsg.buildSuccessResultMsg(jwtToken);
    }

    /**
     * 退出登陆
     * @param userLogoutVo 退出登陆用户信息
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "/logout", notes = "退出登陆")
    public ResultResponseMsg logout(@RequestBody UserLogoutVo userLogoutVo, HttpSession httpSession){
        if(StringUtils.isEmpty(userLogoutVo.getToken())){
            return ResultResponseMsg.buildResultMsg(ErrorEnum.PARAM, null);
        }
        loginService.userLogout(userLogoutVo.getToken(),httpSession.getId());
        return ResultResponseMsg.buildSuccessResultMsg();
    }

    /**
     * 用户注册服务
     * @return
     */
    @PostMapping("/register")
    public ResultResponseMsg register(@RequestBody UserLoginVo userLoginVo, HttpSession httpSession){
        String username = userLoginVo.getUsername();
        String password = userLoginVo.getPassword();
        String captcha = userLoginVo.getCaptcha();
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(captcha)){
            return ResultResponseMsg.buildResultMsg(ErrorEnum.PARAM, null);
        }
        String token = loginService.userRegister(username, password, captcha, httpSession.getId());
        return StringUtils.isEmpty(token) ? ResultResponseMsg.buildFailureResultMsg() : ResultResponseMsg.buildSuccessResultMsg(token);
    }

    /**
     * 用户更新
     * @return
     */
    @PostMapping("/update")
    public ResultResponseMsg update(){
        return ResultResponseMsg.buildSuccessResultMsg();
    }

    /**
     * 用户注销
     * @return
     */
    @PostMapping("/delete")
    public ResultResponseMsg delete(){
        return ResultResponseMsg.buildSuccessResultMsg();
    }
}
